package sim.ring.gui.treedata;

import java.util.ArrayList;
import java.util.List;

public class Folder extends DataModel {
	
	protected List<Folder> folders;
	protected List<Category> categories;
	protected List<TMethod> methods;
	
	private static IDataModelVisitor adder = new Adder();
	private static IDataModelVisitor remover = new Remover();

	public List<Category> getCategories() {
		return categories;
	}

	public void setCategories(List<Category> categories) {
		this.categories = categories;
	}

	public Folder() {
		folders = new ArrayList<Folder>();
		categories = new ArrayList<Category>();
		methods = new ArrayList<TMethod>();
	}
	
	public Folder(String folderName, int id) {
		this();
		this.name = folderName;
		this.id = id;
	}
	
	public Folder(String folderName) {
		this();
		this.name = folderName;
	}
	
	public void addMethod(TMethod newMethod) {
		methods.add(newMethod);
		newMethod.parent = this;
		fireAddEvent(newMethod);
	}
	
	public void removeMethod(TMethod delMethod) {
		methods.remove(delMethod);
		fireRemoveEvent(delMethod);
	}
	
	public void addCategory(Category newCategory) {
		categories.add(newCategory);
		newCategory.parent = this;
		fireAddEvent(newCategory);
	}
	
	public void removeCategory(Category delCategory) {
		categories.remove(delCategory);
		fireRemoveEvent(delCategory);
	}
	
	public void addFolder(Folder newFolder) {
		folders.add(newFolder);
		newFolder.parent = this;
		fireAddEvent(newFolder);
	}
	
	public void removeFolder(Folder delFolder) {
		folders.remove(delFolder);
		delFolder.addChangeListener(DataModelChangeListener.getInstance());
		fireRemoveEvent(delFolder);
	}
	
	public List<Folder> getFolders() {
		return folders;
	}

	public List<TMethod> getMethods() {
		return methods;
	}

	public int size() {
		return methods.size() + categories.size() + folders.size();
	}

	@Override
	public void accept(IDataModelVisitor visitor, Object args) {
		visitor.visitFolder(this, args);
	}
	
	public void add(DataModel model) {
		model.accept(adder, this);
	}
	
	public void remove(DataModel model) {
		model.accept(remover, this);
	}
	
	private static class Adder implements IDataModelVisitor {

		@Override
		public void visitTMethod(TMethod method, Object args) {
			((Folder)args).addMethod(method);
		}

		@Override
		public void visitCategory(Category category, Object args) {
			((Folder)args).addCategory(category);
		}

		@Override
		public void visitFolder(Folder folder, Object args) {
			((Folder)args).addFolder(folder);
		}
		
	}
	
	private static class Remover implements IDataModelVisitor {

		@Override
		public void visitTMethod(TMethod method, Object args) {
			((Folder)args).removeMethod(method);
		}

		@Override
		public void visitCategory(Category category, Object args) {
			((Folder)args).removeCategory(category);
		}

		@Override
		public void visitFolder(Folder folder, Object args) {
			((Folder)args).removeFolder(folder);
			folder.addChangeListener(DataModelChangeListener.getInstance());
		}
		
	}
	
}
